﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace XnaTools
{
    public class MouseState : IDisposable
    {
        private readonly FrameworkElement _attachedElement;

        public MouseState(FrameworkElement attachedElement){
            _attachedElement = attachedElement;
            attachedElement.MouseEnter += OnMouseEnter;
            attachedElement.MouseLeave += OnMouseLeave;
            attachedElement.MouseMove += OnMouseMove;
            attachedElement.MouseLeftButtonDown += OnLeftDown;
            attachedElement.MouseLeftButtonUp += OnLeftUp;
            attachedElement.MouseRightButtonDown += OnRightDown;
            attachedElement.MouseRightButtonUp += OnRightUp;
            attachedElement.MouseWheel += OnWheel;
        }

        public int WheelTicks { get; set; }
        public bool RightButton { get; set; }
        public bool LeftButton { get; set; }
        public float X { get; set; }
        public float Y { get; set; }

        private void OnWheel(object sender, MouseWheelEventArgs e){
            WheelTicks += e.Delta;
        }

        private void OnRightUp(object sender, MouseButtonEventArgs e){
            if(!LeftButton)
                _attachedElement.ReleaseMouseCapture();
            RightButton = false;
        }

        private void OnRightDown(object sender, MouseButtonEventArgs e){
            _attachedElement.CaptureMouse();
            RightButton = true;
        }

        private void OnLeftUp(object sender, MouseButtonEventArgs e)
        {
            if (!RightButton)
                _attachedElement.ReleaseMouseCapture();
            LeftButton = false;
        }

        private void OnLeftDown(object sender, MouseButtonEventArgs e){
            _attachedElement.CaptureMouse();
            LeftButton = true;
        }

        private void OnMouseMove(object sender, MouseEventArgs e){
            var pos = e.GetPosition(_attachedElement);
            X = (float)pos.X;
            Y = (float)pos.Y;
        }

        private void OnMouseLeave(object sender, MouseEventArgs e){

        }

        private void OnMouseEnter(object sender, MouseEventArgs e)
        {
            var pos = e.GetPosition(_attachedElement);
            X = (float)pos.X;
            Y = (float)pos.Y;
        }

        public void Dispose()
        {
            _attachedElement.MouseEnter -= OnMouseEnter;
            _attachedElement.MouseLeave -= OnMouseLeave;
            _attachedElement.MouseMove -= OnMouseMove;
            _attachedElement.MouseLeftButtonDown -= OnLeftDown;
            _attachedElement.MouseLeftButtonUp -= OnLeftUp;
            _attachedElement.MouseRightButtonDown -= OnRightDown;
            _attachedElement.MouseRightButtonUp -= OnRightUp;
            _attachedElement.MouseWheel -= OnWheel;
        }
    }
}
